package cn.four.hqServer.modules.hqtranuser.controller;

import java.text.ParseException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import cn.four.hqServer.common.entity.HqTranUser;
import cn.four.hqServer.common.result.Result;
import cn.four.hqServer.common.utils.DateUtil;
import cn.four.hqServer.modules.hqtranuser.service.ShzIHqTranUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author znb
 * @since 2023-02-23
 */
@Api(tags = "获取交易信息")
@Validated
@RestController
@RequestMapping("/transuser")
@CrossOrigin
public class ShzHqTranUserController {

	@Autowired
	private ShzIHqTranUserService tranUserService;

	

	@GetMapping("/getTransaction")
	@ApiOperation(value = "获取交易信息")
	public JSONObject getTransaction(@RequestParam(required = false) String startTime,
			@RequestParam(required = false) String endTime, @RequestParam(required = false) String type,
			@RequestParam(required = false) String payee,
			@RequestParam(required = false) int pageNum,
			@RequestParam(required = false) int pageSize,
			@RequestParam(required = false) String userid
			) throws ParseException {
		JSONObject json =new JSONObject();
		json.put("code", 201);
		if (StringUtils.isNotBlank(startTime) && StringUtils.isBlank(endTime)) {

			json.put("msg", "开始时间和结束时间不能只填写一个");
			return json;
		}
		if (StringUtils.isBlank(startTime) && StringUtils.isNotBlank(endTime)) {
			
			json.put("msg", "开始时间和结束时间不能只填写一个");
			return json;
		}
		if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
			Double h = DateUtil.shijiancha(startTime, endTime);
			if (h < 0) {
				
				json.put("msg", "开始时间不能大于结束时间");
				return json;
			}
			if (h > 24 * 30 * 3) {
				
				json.put("msg", "时间间隔不可大于3个月");
				return json;
			}
		}
		int pagestart =(pageNum-1)*pageSize ;
		int pageend =pageSize;
		List<HqTranUser> transaction = tranUserService.getTransaction(startTime, endTime, type, payee,pagestart,pageend,userid);
		int num = tranUserService.getTransactionnum(startTime, endTime, type, payee,pagestart,pageend,userid);
		json.put("data", transaction);
		json.put("count", num);
		json.put("code", 200);
		json.put("msg", "查询成功");
		return json;
	}
	
	
	
	@GetMapping("/getTransactionold")
	@ApiOperation(value = "获取交易信息")
	public Result<List<HqTranUser>> getTransactionold(@RequestParam(required = false) String startTime,
			@RequestParam(required = false) String endTime, @RequestParam(required = false) String type,
			@RequestParam(required = false) String payee,
			@RequestParam(required = false) int pageNum,
			@RequestParam(required = false) int pageSize,
			@RequestParam(required = false) String userid			
			) throws ParseException {
		System.out.println("==============进来了");
		if (StringUtils.isNotBlank(startTime) && StringUtils.isBlank(endTime)) {
			return Result.error(201, "开始时间和结束时间不能只填写一个");
			
		}
		if (StringUtils.isBlank(startTime) && StringUtils.isNotBlank(endTime)) {
			return Result.error(201, "开始时间和结束时间不能只填写一个");
			
		}
		if (StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime)) {
			Double h = DateUtil.shijiancha(startTime, endTime);
			if (h < 0) {
				return Result.error(201, "开始时间不能大于结束时间");
			}
			if (h > 24 * 30 * 3) {
				return Result.error(201, "时间间隔不可大于3个月");
			}
		}
		int pagestart =(pageSize-1)*pageNum +1;
		int pageend =pageSize;
		List<HqTranUser> transaction = tranUserService.getTransaction(startTime, endTime, type, payee,pagestart,pageend,userid);
		return Result.success(transaction);
	}
}
